import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '../views/Layout.vue'
import Login from '../views/login.vue'

const routes = [
  {
    path:'/',
    name:'login',
    // meta:{requireAuth:true},
    component: Login,
    meta:{title:'爱听原产地报表系统'}
  },
  {
    path: '/Layout',
    name: 'Layout',
    component: Layout,
    meta:{
      tittle:'首页',
      isLogin: true,
    },
    children:[
      {
        path:'/Home',
        name:'Home',
        //component:Home,
        children:[
          {
            path:'radioAndProgram',
            name:'radioAndProgram',
            meta:{
              tittle:'电台与节目管理',
              isLogin: true,
            },
            component: () => import('../views/RadioAndProgram.vue')
          },
          {
            path:'dailyRadioAndProgram',
            name:'dailyRadioAndProgram',
            meta:{
              tittle:'每日电台与节目管理',
              isLogin: true,
            },
            component: () => import('../views/RadioAndProgramDaily.vue')
          },
          {
            path:'dailyRadioAndProduct',
            name:'dailyRadioAndProduct',
            meta:{
              tittle:'每日电台与主品管理',
              isLogin: true,
            },
            component: () => import('../views/RadioAndProductDaily.vue')
          }
        ],
      },
      {
        path:'/outside',
        name:'outside',
        //component: () => import('../views/Outside.vue'),
        children:[
          {
            path:'Order',
            name:'Order',
            meta:{
              tittle:'订单表管理',
              isLogin: true,
            },
            component: () => import('../views/Order.vue')
          },
          {
            path:'Secondary',
            name:'Secondary',
            component: () => import('../views/Secondary.vue')
          },
          {
            path:'MediaSales',
            name:'MediaSales',
            component: () => import('../views/MediaSales.vue')
          }
        ]
      },
      {
        path:'/graphical',
        name:'graphical',
        //component: () => import('../views/graphical.vue'),
        children:[
          {    
              path:'totalSales',
              name:'totalSales',
              meta:{
                tittle:'销售额排名',
                isLogin: true,
              },
              component: () => import('../views/TotalSales.vue')           
          },
          {             
              path:'radioTop',
              name:'radioTop',
              component: () => import('../views/RadioTop.vue')           
          },
          {
              path:'floatingline',
              name:'floatingline',
              component: () => import('../views/Floatingline.vue')   
          }       
        ]
      }
    ]

  },
  // {
  //   path: '/tab',
  //   name: 'tab',
  //   //这是异步加载方式，除了首页其他页面都要这么写
  //   component: () => import('../components/Tab.vue')
  // }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// 配置路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    // 这么写每个页在切换的时候可以有自己的title
    //document.title = to.meta.title
    // 设个全局的标题
    document.title = '爱听原产地报表系统'
  }
  if( to.matched.some( ele => ele.meta.isLogin ) ){  //如果为 true 表示需要登陆
       //判断登陆
       if(sessionStorage.getItem('userinfo')){
        //console.log("已登录")
        next()
       }else{
         next('/')
       }
  }else{
    next()
  }
});

export default router